//
//  FLLCommunityViewController.m
//  FLLGodOfCookery
//
//  Created by LL.F on 16/8/27.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLCommunityViewController.h"
#import "FLLAttentionViewController.h"
#import "FLLPresentViewController.h"
#import "FLLNewestViewController.h"
#import "FLLMacro.h"
#import "UIView+Frame.h"
#import "FLLActivityViewController.h"

@interface FLLCommunityViewController ()<UIScrollViewDelegate>
/**
 *  scrollView
 */
@property (nonatomic, strong) UIScrollView *communityScrollView;
/**
 *  当前VC
 */
@property (nonatomic, strong) UIViewController *currentViewController;
/**
 *  自定义Navigation的titleView
 */
@property (nonatomic, strong) UIView *titleView;
// titleArray
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIButton *selectedButton;
@end

@implementation FLLCommunityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createChildViewController];
    [self createCommunityScrollView];
    // 创建titleView
    self.titleArray = [NSArray arrayWithObjects:@"关注", @"推荐", @"最新", nil];
    [self createTitleView];
    [self createNavigationBarButton];
    
}
// 创建子视图控制器
- (void)createChildViewController
{
    FLLAttentionViewController *attentionVC = [[FLLAttentionViewController alloc] init];
    [self addChildViewController:attentionVC];
    FLLPresentViewController *presentVC = [[FLLPresentViewController alloc] init];
    [self addChildViewController:presentVC];
    FLLNewestViewController *newestVC = [[FLLNewestViewController alloc] init];
    [self addChildViewController:newestVC];

}
// 创建scrollView
- (void)createCommunityScrollView
{

    self.communityScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49)];
    [self.view addSubview:_communityScrollView];
    _communityScrollView.contentSize = CGSizeMake(self.childViewControllers.count * SCREEN_WIDTH, 0);
    [_communityScrollView setContentOffset:CGPointMake(SCREEN_WIDTH * 2, 0) animated:YES];
    _communityScrollView.pagingEnabled = YES;
    _communityScrollView.delegate = self;
    // ????????????
    [self.view insertSubview:_communityScrollView atIndex:0];
    [self scrollViewDidEndScrollingAnimation:_communityScrollView];
    
}
//  将VC的View带到scrollView的上面
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    NSInteger i = scrollView.contentOffset.x / SCREEN_WIDTH;
    self.currentViewController = self.childViewControllers[i];
    _currentViewController.view.frame = CGRectMake(scrollView.contentOffset.x, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49);
    [scrollView addSubview:_currentViewController.view];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self scrollViewDidEndScrollingAnimation:scrollView];
    NSInteger index = scrollView.contentOffset.x / SCREEN_WIDTH;
    [self didClickedTitleButton:_titleView.subviews[index]];
}
// 创建titleView
- (void)createTitleView
{
    self.titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    self.lineView = [[UIView alloc] init];
    _lineView.backgroundColor = FLLOrangeColor;
    _lineView.fll_height = 3;
    for (int i = 0; i < _titleArray.count; i++) {
        UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        titleButton.frame = CGRectMake(_titleView.frame.size.width / 3 * i, 0, _titleView.frame.size.width / 3, _titleView.frame.size.height - 3);
        [self.titleView addSubview:titleButton];
        [titleButton setTitle:_titleArray[i] forState:UIControlStateNormal];
        [titleButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [titleButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateDisabled];
        titleButton.tag = 1000 + i;
        titleButton.titleLabel.font = [UIFont systemFontOfSize:17 * kWidthScale];
        [titleButton addTarget:self action:@selector(didClickedTitleButton:) forControlEvents:UIControlEventTouchUpInside];
        _lineView.fll_y = titleButton.frame.size.height;
        if (i == 1) {
            //   让按钮的lable根据内容计算尺寸
            [titleButton.titleLabel sizeToFit];
            [self didClickedTitleButton:titleButton];
            _lineView.fll_width = titleButton.titleLabel.fll_width;
            _lineView.fll_centerX = titleButton.center.x;
        }
    }
    [self.titleView addSubview:_lineView];
    self.navigationItem.titleView = _titleView;
}
- (void)didClickedTitleButton:(UIButton *)button
{
    
    self.selectedButton.enabled = YES;
    button.enabled = NO;
    [UIView animateWithDuration:0.25 animations:^{
        self.lineView.fll_width = button.titleLabel.fll_width;
        self.lineView.fll_centerX = button.fll_centerX;
    }];
    self.selectedButton = button;
    NSInteger i = button.tag - 1000;
    CGPoint index = self.communityScrollView.contentOffset;
    index.x = i * SCREEN_WIDTH;
    [self.communityScrollView setContentOffset:index animated:YES];
    
}
- (void)createNavigationBarButton
{

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"activity"]  style:UIBarButtonItemStylePlain target:self action:@selector(didClickedActivityButton:)];
}
// "活动"barButtonItem
- (void)didClickedActivityButton:(UIBarButtonItem *)button
{

    FLLActivityViewController *activityVC = [[FLLActivityViewController alloc] init];
    activityVC.navigationItem.title = @"活动";
    [self.navigationController pushViewController:activityVC animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
